package com.seis_645.appTesting;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {
	
	private static final String DATABASE_NAME = "TEST_DRIVE.db";
	private static final int DATABASE_VERSION = 4;
	public static final String TBL_CAR = "car";
	public static final String TBL_DEALER = "dealer";
	public static final String TBL_SALESPERSON = "salesperson";
	public static final String TBL_ADDRESS = "address";
	
	// Columns for Address Table
	public static final String ADDR_ID = "_id";
	public static final String ADDR_STREET = "street";
	public static final String ADDR_CITY = "city";
	public static final String ADDR_STATE = "state";
	public static final String ADDR_ZIP = "zip";
	
	private static final String create_address = "create table " + TBL_ADDRESS +
			"( _id integer primary key autoincrement, " +
			"street text, city text, state text, zip text );";
	
	// Columns for Car Table
	public static final String CAR_ID = "_id";
	public static final String CAR_YEAR = "year";
	public static final String CAR_MAKE = 	"make";
	public static final String CAR_MODEL = "model";
	public static final String CAR_PKAGE = "pkage";
	public static final String CAR_GMHWY = "gmHwy";
	public static final String CAR_GMCITY = "gmCity";
	public static final String CAR_PRICE = "price";
	public static final String CAR_DRIVEABILITY = "driveability";
	public static final String CAR_COMFORT ="comfort";
	public static final String CAR_APPEARANCE = "appearance";
	public static final String CAR_SAFETY ="safety";
	public static final String CAR_ACCESSORIES ="accessories";
			
	
	
	private static final String create_car = "create table " + TBL_CAR +
			"( _id integer primary key autoincrement, " +
			"year integer, make text, model text, pkage text, gmHwy integer, gmCity integer, price integer, driveability integer," +
			"comfort integer, appearance integer, safety integer, accessories integer);";
	
	// Columns for Dealer table
	public static final String DLR_ID = "_id";
	public static final String DLR_NAME = "name";
	public static final String DLR_ADDR_ID = "addr_id";
	public static final String DLR_CLEANLINESS = "cleanliness";
	public static final String DLR_FRIENDLINESS = "friendliness";
	public static final String DLR_SUPPLY = "supply";
	
	private static final String create_dealer = "create table " + TBL_DEALER + 
			"( _id integer primary key autoincrement, " +
			"name text, addr_id integer, cleanliness integer, friendliness integer, supply integer);";
	
	// Columns for Salesperson table
	public static final String SLP_ID = "_id";
	public static final String SLP_FNAME = "f_name";
	public static final String SLP_LNAME = "l_name";
	public static final String SLP_DLR_ID = "dealer_id";
	
	private static final String create_salesperson = "create table " + TBL_SALESPERSON + 
			" ( _id integer primary key autoincrement," +
			" f_name text, l_name text, " +
			" dealer_id integer); ";
	
	public MySQLiteHelper(Context context) {
		super(context, DATABASE_NAME,null,DATABASE_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(create_address);
		database.execSQL(create_car);
		database.execSQL(create_dealer);
		database.execSQL(create_salesperson);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(MySQLiteHelper.class.getName(),
				"Upgrading from version" + oldVersion + " to " + newVersion + ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS "+ TBL_ADDRESS);
		db.execSQL("DROP TABLE IF EXISTS " + TBL_SALESPERSON);
		db.execSQL("DROP TABLE IF EXISTS " + TBL_DEALER);
		db.execSQL("DROP TABLE IF EXISTS " + TBL_CAR);
		onCreate(db);
		
	}
	

}
